<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin\TvmMobileDetail;
use App\Models\Admin\TvmMobileCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\AdminAction;


class SystemController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}

	/**
	 * 管理员行为日志记录列表
	 **/
	public function admin_action_list(Request $request) {
		//如果是post请求，则执行搜索操作
		if($request->isMethod('post')) {
			$node_code = $request->input('node_code');
			$data = DB::table('admin_action')
					->where('node_code','like',$node_code.'%')
					->orderBy('action_id','desc')
					->paginate(20);		
		}else {
			//查询管理员行为日志信息
			$data = DB::table('admin_action')->orderBy('action_id','desc')->paginate(20);
		}
		
		//解析请求参数信息
		foreach($data as $key => $value) {
			$data[$key]->request = json_decode($value->request_info,true);
		}
		//展示模板
		return view('admin.system.admin_action_list',compact('data','node_code'));
	}

	/**
	 * 管理员行为日志详情
	 **/
	public function admin_action_detail(Request $request) {
		//查询管理员行为日志信息（ID号）
		$data = DB::table('admin_action')->where('action_id',$request->input('id'))->first();
		//解析请求参数信息
		$data->request = json_decode($data->request_info,true);
		//展示模板
		return view('admin.system.admin_action_detail',compact('data'));
	}



	/**
	 * 管理员审核申请列表
	 **/
	public function apply_list(Request $request) {
		$condition = array();
		$keywords = $request->input('keywords');
		$status = $request->input('status');
		if($keywords){
			$condition[] = ['manage_cate', 'like', '%'.$keywords.'%'];
		}
		switch ($status) {
			case '0':
				$condition[] = ['status','=',0];
				break;
			case '1':
			$condition[] = ['status','=',1];
				break;
			case '2':
			$condition[] = ['status','=',2];
				break;	
			default:
				$condition[] = ['status','=',1];
				break;
		}
		
		
		//查询管理员审核信息
			$data = DB::table('oshop_apply_detail')->where($condition)->orderBy('id','desc')->paginate(20);
		
		//展示模板
		return view('admin.system.apply_list',[
			'data' => $data,
			'keywords' => $keywords
		]);
	}



	/**
	 * 管理员审核页面
	 **/

	public function apply_action(Request $request){
		$id = $request->input('id');
		return view('admin.system.apply_form',[
			'id' => $id
		]);
	}
	//审核通过提交
	public function apply_save(Request $request) {
		$remarks = $request->input('remarks');
		$status = $request->input('status');
		$id = $request->input('id');
		if($status==1){
			$data['status'] = 2;
			$data['remarks'] = $remarks;
			$data['audit_id'] = $_SESSION['admin_uid'];
			$data['audit_time'] = date('Y-m-d H:i:s',time());
		}else{
			$data['status'] = 0;
			$data['remarks'] = $remarks;
			$data['audit_id'] = $_SESSION['admin_uid'];
			$data['audit_time'] = date('Y-m-d H:i:s',time());
		}
		
		$result = DB::table('oshop_apply_detail')->where('id',$id)->update($data);
		echo 1;
	}

	/**
	 * @param Request $request
	 * 信息编辑页面
	 */

	/**
	 * @param Request $request
	 * 手机型号编辑
	 */
	public function apply_edit(Request $request){

		if($request->isMethod('post')){
			$data = array();
			$id = $request->input('id');
			if(!$id){
				$this->ajax_error('ID为空');
			}
			if($manage_cate = $request->input('manage_cate')){
				$data['manage_cate'] = $manage_cate;
			}else{
				$this->ajax_error('店铺经营类别不能为空');
			}
			if($pic1 = $request->input('pic1')){
				$data['shop_photo'] = $pic1;
			}else{
				$this->ajax_error('门店照片不能为空');
			}
			if($pic2 = $request->input('pic2')){
				$data['business_license'] = $pic2;
			}else{
				$this->ajax_error('营业执照图片不能为空');
			}
			
			if($shop_address = $request->input('shop_address')){ //地址
				$data['shop_address'] = $shop_address;
			}
			if($login_account = $request->input('login_account')){ //注册账号
				$data['login_account'] = $login_account;
			}
			if($profit_percent = $request->input('profit_percent')){  //让利折扣
				$data['profit_percent'] = $profit_percent;
			}
			if($corporation_name = $request->input('corporation_name')){ //法人用户名
				$data['corporation_name'] = $corporation_name;
			}
			if($corporation_idcard = $request->input('corporation_idcard')){ //法人身份证
				$data['corporation_idcard'] = $corporation_idcard;
			}
			if($corporation_phone = $request->input('corporation_phone')){ //法人手机号
				$data['corporation_phone'] = $corporation_phone;
			}
			if($bank_name = $request->input('bank_name')){ //法人银行开户名
				$data['bank_name'] = $bank_name;
			}
			if($bank_cate = $request->input('bank_cate')){ //银行名称
				$data['bank_cate'] = $bank_cate;
			}
			if($bank_branch = $request->input('bank_branch')){//银行支行
				$data['bank_branch'] = $bank_branch;
			}
			if($bank_card_no = $request->input('bank_card_no')){ //银行卡号
				$data['bank_card_no'] = $bank_card_no;
			}
			if($master_name = $request->input('master_name')){ //对接人用户名
				$data['master_name'] = $master_name;
			}
			if($master_phone = $request->input('master_phone')){ //对接人手机
				$data['master_phone'] = $master_phone;
			}
			if($master_email = $request->input('master_email')){ //对接人邮箱
				$data['master_email'] = $master_email;
			}
			if($master_qq = $request->input('master_qq')){ //对接人QQ
				$data['master_qq'] = $master_qq;
			}

			$result = DB::table('oshop_apply_detail')->where('id',$id)->update($data);

			if(!$result){
				$this->ajax_error('修改失败');
			}
			$this->ajax_success('修改成功');
		}

		$id = $request->input('id');
		//编辑页面信息
		$productInfo = DB::table('oshop_apply_detail')->where('id',$id)->first(); //申请信息

		return view('admin.system.apply_edit',[
			'data' => $productInfo,
			'action' => 'apply_edit'
		]);
	}


}